package com.tzq.bmsadmin.controller;

import com.tzq.bmsadmin.entity.User;
import com.tzq.bmsadmin.mapper.UserMapper;
import com.tzq.bmsadmin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;



    @PostMapping
    public Integer save(@RequestBody User user){ //新增或者更新
       return userService.save(user);
    }

    @GetMapping
    public List<User> index(){//查询所有数据
        List<User> all = userMapper.findAll();
        return all;
    }

    @DeleteMapping("/{id}")
    public Integer delete(@PathVariable Integer id){
      return   userMapper.deleteById(id);
    }


    //分页查询接口
    //接口路径/user/page
    //@RequestParam接收 ?pageNum=1&pageSize=10
    @GetMapping("/page")
    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize){//查询所有数据
        pageNum = (pageNum - 1) * pageSize;
        List<User> data = userMapper.selectPage(pageNum, pageSize);
        Integer total = userMapper.selectTotal();
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }
}
